/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.action.pay;

import org.zhiqim.account.ZacConstants;
import org.zhiqim.account.ZacSessionUser;
import org.zhiqim.account.api.pay.AlipayServer;
import org.zhiqim.account.dbo.user.UserTradeAlipay;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.ValidateAction;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.httpd.validate.onex.IsAmount2R;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.util.Ids;
import org.zhiqim.kernel.util.Sqls;
import org.zhiqim.orm.ZTable;

/**
 * 生成支付跳转页面
 *
 * @version v1.0.0 @author yangpeng 2018-2-26 新建与整理
 */
public class AlipayAction extends ValidateAction implements ZacConstants
{
    @Override
    protected void validate(HttpRequest request)
    {
        request.addValidate(new IsAmount2R("tradeAmount", "交易金额不能为空且必须为金额格式"));
        request.addValidate(new IsNotEmpty("tradeSubject", "交易标题不能为空"));
    }

    @Override
    protected void perform(HttpRequest request) throws Exception
    {
        if (request.isMethodGet())
        {
            request.returnHistory();
            return;
        }
        
        ZacSessionUser sessionUser = request.getSessionUser(ZacSessionUser.class);
        
        long tradeId = Ids.longId(PAY_SEQ);
        int tradeAmount = request.getParameterAmount2R("tradeAmount");
        String tradeSubject = request.getParameter("tradeSubject");
        String tradeCallback = request.getParameter("tradeCallback");
        long tradeOrderId = request.getParameterLong("tradeOrderId", 0);
        
        //支付前六项，用户编号、交易编号、交易金额、交易标题，和交易状态&交易创建时间
        UserTradeAlipay trade = new UserTradeAlipay();
        trade.setUserId(sessionUser.getUserId());
        trade.setTradeId(tradeId);
        trade.setTradeAmount(tradeAmount);
        trade.setTradeSubject(tradeSubject);
        trade.setTradeCallback(tradeCallback);
        trade.setTradeOrderId(tradeOrderId);
        trade.setTradeStatus(TRADE_WAITING);
        trade.setTradeCreated(Sqls.nowTimestamp());
        
        Global.get(ZTable.class).insert(trade);
        
        if (request.isMobile())
        {//手机端
            String form = Global.getService(AlipayServer.class).tradeWapPay(tradeId, tradeAmount, tradeSubject);
            request.getResponse().print(form);
        }
        else
        {//电脑端
            String form = Global.getService(AlipayServer.class).tradePagePay(tradeId, tradeAmount, tradeSubject);
            request.getResponse().print(form);
        }
    }
}
